package com.saltfish.appengine.worktable;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class CalendarServlet extends ActionServlet {

	@Override
	protected void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String ym = req.getParameter("selYm");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		try {
			req.setAttribute("selYmList", selectYmList(sdf.parse(ym)));
		} catch (ParseException e1) {
		}

		forward("/calendar.jsp");
	}
	
	private Map<String, String> selectYmList(Date selYm) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar cal = Calendar.getInstance();
		Map<String, String> map = new TreeMap<String, String>();
		for (int i = 0; i < 12; i++) {
			cal.setTime(new Date());
			cal.add(Calendar.MONTH, -i);
			String ym = sdf.format(cal.getTime());
			String check = "";
			if (ym.equals(sdf.format(selYm))) {
				check = "check";
			}
			map.put(ym, check);
		}
		return map;
	}
}
